/*
 * @(#)SetCookieMethod.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.macro.method.component;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * @author Web Platform Development Team
 */
public class SetCookieMethod implements TemplateMethodModelEx {
    Log log = LogFactory.getLog(GetCookieMethod.class);

    /**
     * @param args the arguments
     * @return boolean the add cookie success
     * @throws TemplateModelException the TemplateModelException
     */
    @SuppressWarnings("unchecked")
	public Object exec(List args) throws TemplateModelException {

        if (args.size() != 3) {
            throw new IllegalArgumentException();
        }

        Object obj = args.get(0);
        obj = BeansWrapper.getDefaultInstance().unwrap((TemplateModel)obj, HttpServletResponse.class);

        if (!(obj instanceof HttpServletResponse)) {
            throw new IllegalArgumentException();
        }

        HttpServletResponse response = (HttpServletResponse)obj;

        String key = args.get(1).toString();
        String value = args.get(2).toString();

        Cookie cookie = new Cookie(key, value);
        response.addCookie(cookie);

        return true;
    }
}
